<template>
  <div class="loglogin-container">
    <div class="loglogin-search">
      <el-form :inline="true" :model="searchform" ref="searchform" class="demo-form-inline">
        <el-form-item label="创建时间" prop="key">
          <div class="block">
            <el-date-picker
              size="small"
              v-model="times"
              value-format="yyyy-MM-dd"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
            ></el-date-picker>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="list">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="loglogin-body">
      <el-table :data="logData" style="width: 100%;margin-bottom: 20px;" row-key="id" border>
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="userName" label="登录用户" width="180"></el-table-column>
        <el-table-column prop="status" label="登录状态" width="180">
          <template v-slot="{row}">
            <el-tag
              size="small"
              :type="row.status ===1?'success':'danger'"
            >{{row.status ===1?'成功':'失败'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="ip" label="IP地址"></el-table-column>
        <el-table-column prop="descript" label="描述信息"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchform.pageNum"
        :page-sizes="[5, 10, 15]"
        :page-size="searchform.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { listLogLoginByPage, delLogLogin } from "@/api/log";
export default {
  name: "log",
  components: {},
  data() {
    return {
      searchform: {
        pageNum: 1,
        pageSize: 5
      },
      total: 0,
      logData: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      times: []
    };
  },
  created() {
    this.list();
  },
  methods: {
    async list() {
      const { data } = await listLogLoginByPage(this.searchform);
      this.logData = data.data;
      this.searchform.pageNum = data.pageNum;
      this.searchform.pageSize = data.pageSize;
      this.total = data.total;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.searchform.pageNum = val;
      this.list(this.searchform);
    },
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          await delLogLogin(row.id);
          if (index === 0) {
            this.searchform.pageNum--;
          }
          this.list();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  },
  watch: {
    times() {
      if (this.times && this.times.length > 0) {
        this.searchform.startTime = this.times[0];
        this.searchform.endTime = this.times[1];
      } else {
        this.searchform.startTime = "";
        this.searchform.endTime = "";
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.loglogin {
  &-container {
    margin: 30px;
  }

  &-search {
    height: 70px;
    padding: 10px 10px;
    border-radius: 5px;
    background-color: #fff;
  }

  &-body {
    border-radius: 5px;
    background-color: #fff;
    margin: 20px 0px;
    padding: 20px 10px;
  }
}
.el-pagination {
  text-align: center;
}
</style>